<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 用户管理
 */
class UserController extends Controller {
    //用户登录
    public function login(){
        $member = M('user');
        $username =addslashes($_POST['username']);
        $password =md5($_POST['password']);
       
        //验证账号密码是否正确
        $user = $member->where("username = '%s' and password= '%s'",array($username,$password))->find();

        if(!$user) {
            $data["data"] = 0;
            echo json_encode($data) ;exit;
        }
        
        //更新登陆信息
        $data =array(
            'id' => $user['id'],
            'updatetime' => time(),
            'login_ip' => get_client_ip(),
        );
        
        //如果数据更新成功  跳转到后台主页
        if($member->save($data)){
            // session('adminId',$user['id']);
            $_SESSION['username']=$user['id'];
            $data["data"] = 1;
            $data["userid"] = $user["id"];
            echo json_encode($data);exit;
        }else{
            $data["data"] = 0;
            echo json_encode($data);exit;
        }
    }
	
	
    public function logout(){
        session_unset($_SESSION['username']);
		$data["userid"] =0;
		$data["data"] = 0;
		echo json_encode($data);
		exit;
        //redirect(U('Index/'));
    }
	
}
